package cn.wx.scholar;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

import org.apache.commons.lang.StringUtils;
import com.csvreader.CsvReader;

import cn.wx.common.utils.JSONUtils;
public class ReadCsvPaperExpert {

	
	public static void main(String[] args) {
		
		final String filePath = "D:\\my_paper_author\\paperAuth.csv";
		try {
			
			File file = new File(filePath);
	        FileInputStream fileInputStream = new FileInputStream(file);  
	        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
			
			CsvReader csvReader = new CsvReader(bufferedInputStream,Charset.forName("utf-8"));
			
			File of = new File("D:\\my_paper_author\\paperAuth1_1.csv");
			FileOutputStream ofs = new FileOutputStream(of,true);
			OutputStreamWriter ofw = new OutputStreamWriter(ofs,"utf-8");
			BufferedWriter output = new BufferedWriter(ofw);

			// 读表头
            csvReader.readHeaders();
            int i = 0;
            while (csvReader.readRecord()){
                // 读一整行
            	String line = csvReader.getRawRecord();
				System.out.println("读取" + (i++) +"行");
				if(StringUtils.isBlank(line)) {
					continue;
				}

				/**
				 * 解析csv
				 */
				String paperId = csvReader.get(0);
				String authors = csvReader.get(1);
				StringBuffer sb = null;
				
				if(StringUtils.isNotBlank(authors)) {
					String[] aes = authors.split(",");
					
					for(String s:aes) {
						if(StringUtils.isBlank(s)) {
							continue;
						}
						
						sb = new StringBuffer();
						sb.append(paperId).append(",").append(JSONUtils.c(s));
						
						output.write(sb.toString());
						output.newLine();
					}
					output.flush();
				}
				
            }
			
            output.flush();
            output.close();
            output = null;
            
            /**
			 * 关闭读入流
			 */
			csvReader.close();
			csvReader = null;
            
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
